//
//  PersonInfoVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/31.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "PersonInfoVC.h"
#import "DriverAddCell.h"
#import "StaffMainModel.h"
#import "AddStaffChooseGenderCell.h"

@interface PersonInfoVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) StaffMainModel *model;

@end

@implementation PersonInfoVC

-(StaffMainModel *)model {
    if (!_model) {
        _model = [StaffMainModel new];
    }
    return _model;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - 64 - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, ScreenW - 60, 44)];
        [saveBtn setBackgroundColor:Color_ThemeColor];
        saveBtn.layer.masksToBounds = YES;
        saveBtn.layer.cornerRadius = 6;
        [saveBtn setTitle:@"保存" forState:0];
        [saveBtn setTitleColor:HexColor(0xffffff) forState:0];
        saveBtn.titleLabel.font = BoldFontOfSize(16);
        [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:saveBtn];
    }
    return _bottomView;
}

- (void)saveClick {
    if ([FactoryMethod isNull:self.model.name]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入姓名"];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.model.name forKey:@"sw_name"];
    [dic setObject:self.model.department forKey:@"sw_department"];
    [dic setObject:self.model.position forKey:@"sw_position"];
    [dic setObject:self.model.sex forKey:@"sw_sex"];
    [HttpRequest PostWithApi:ReceiptDeliverySetPersonInfoSave params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"保存成功"];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self requestForGetInfo];
}

- (void)requestForGetInfo {
    [HttpRequest PostWithApi:ReceiptDeliverySetPersonInfoGet params:@{} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model.name = [responseObject objectForKey:@"name"];
            self.model.sex = [responseObject objectForKey:@"sex"];
            self.model.department = [responseObject objectForKey:@"department"];
            self.model.position = [responseObject objectForKey:@"position"];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
    if (indexPath.row == 0) {
        cell.titleText = @"姓名";
        cell.showLine = YES;
        cell.canClick = NO;
        cell.isMust = YES;
        cell.contentText = self.model.name;
        __weak_self;
        cell.cellBlock = ^(NSString * _Nonnull str) {
            weakself.model.name = str;
        };
    }else if (indexPath.row == 1) {
        AddStaffChooseGenderCell *cell = [AddStaffChooseGenderCell AddStaffChooseGenderCellWithTableView:tableView];
        cell.isMan = [self.model.sex isEqualToString:@"1"] ? YES : NO;
        cell.showLine = YES;
        __weak_self;
        cell.changeBlock = ^{
            if ([weakself.model.sex isEqualToString:@"1"]) {
                weakself.model.sex = @"2";
            }else {
                weakself.model.sex = @"1";
            }
            [weakself.tableView reloadData];
        };
        return cell;
    }else if (indexPath.row == 2) {
        cell.titleText = @"部门";
        cell.showLine = YES;
        cell.canClick = NO;
        cell.isMust = NO;
        cell.contentText = self.model.department;
        __weak_self;
        cell.cellBlock = ^(NSString * _Nonnull str) {
            weakself.model.department = str;
        };
    }else if (indexPath.row == 3) {
        cell.titleText = @"岗位";
        cell.showLine = NO;
        cell.canClick = NO;
        cell.isMust = NO;
        cell.contentText = self.model.position;
        __weak_self;
        cell.cellBlock = ^(NSString * _Nonnull str) {
            weakself.model.position = str;
        };
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        AddStaffChooseGenderCell *cell = [AddStaffChooseGenderCell AddStaffChooseGenderCellWithTableView:tableView];
        return cell.height;
    }
    DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
        view.backgroundColor = Color_backgroundColor;
        return view;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    }
    return 0;
}

@end
